const AuthService = require('../services/authService');

class AuthController {
  /**
   * 用户登录
   * @param {Koa.Context} ctx - Koa上下文
   */
  static async login(ctx) {
    try {
      const { username, password, captcha } = ctx.request.body;
      
      const result = await AuthService.login(username, password, captcha);
      
      ctx.body = {
        success: true,
        data: result,
        message: '登录成功'
      };
    } catch (error) {
      ctx.status = error.status || 400;
      ctx.body = {
        success: false,
        message: error.message || '登录失败'
      };
    }
  }

  /**
   * 用户注册
   * @param {Koa.Context} ctx - Koa上下文
   */
  static async register(ctx) {
    try {
      const userData = ctx.request.body;
      
      const result = await AuthService.register(userData);
      
      ctx.status = 201;
      ctx.body = {
        success: true,
        data: result,
        message: '注册成功'
      };
    } catch (error) {
      ctx.status = error.status || 400;
      ctx.body = {
        success: false,
        message: error.message || '注册失败'
      };
    }
  }

  /**
   * 刷新令牌
   * @param {Koa.Context} ctx - Koa上下文
   */
  static async refreshToken(ctx) {
    try {
      const { refreshToken } = ctx.request.body;
      
      const result = await AuthService.refreshToken(refreshToken);
      
      ctx.body = {
        success: true,
        data: result,
        message: '令牌刷新成功'
      };
    } catch (error) {
      ctx.status = error.status || 401;
      ctx.body = {
        success: false,
        message: error.message || '令牌刷新失败'
      };
    }
  }

  /**
   * 用户登出
   * @param {Koa.Context} ctx - Koa上下文
   */
  static async logout(ctx) {
    try {
      const { refreshToken } = ctx.request.body || {};
      const userId = ctx.user?.id;
      
      await AuthService.logout(userId, refreshToken);
      
      ctx.body = {
        success: true,
        message: '登出成功'
      };
    } catch (error) {
      ctx.status = error.status || 400;
      ctx.body = {
        success: false,
        message: error.message || '登出失败'
      };
    }
  }

  /**
   * 重置密码
   * @param {Koa.Context} ctx - Koa上下文
   */
  static async resetPassword(ctx) {
    try {
      const { username, newPassword, resetCode } = ctx.request.body;
      
      await AuthService.resetPassword(username, newPassword, resetCode);
      
      ctx.body = {
        success: true,
        message: '密码重置成功'
      };
    } catch (error) {
      ctx.status = error.status || 400;
      ctx.body = {
        success: false,
        message: error.message || '密码重置失败'
      };
    }
  }

  /**
   * 生成验证码
   * @param {Koa.Context} ctx - Koa上下文
   */
  static async generateCaptcha(ctx) {
    try {
      const result = await AuthService.generateCaptcha();
      
      ctx.body = {
        success: true,
        data: result,
        message: '验证码生成成功'
      };
    } catch (error) {
      ctx.status = error.status || 500;
      ctx.body = {
        success: false,
        message: error.message || '验证码生成失败'
      };
    }
  }
}

module.exports = AuthController;